package apptoko.com;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.Toast;
import apptoko.com.adapter.ErrorHandler;
import apptoko.com.background.BackgroundTasks;
import apptoko.com.event.Event.CardFailEvent;
import apptoko.com.event.Event.CardSuccessEvent;
import apptoko.com.event.Event.RequestFailEvent;
import apptoko.com.network.ProgressDialogManager;
import apptoko.com.util.Constants;
import apptoko.com.util.Utils;
import apptoko.com.validator.Form;
import apptoko.com.validator.Validate;
import apptoko.com.validator.validator.NotEmptyValidator;
import apptoko.com.view.AlertDialogManager;

import com.androidquery.AQuery;
import apptoko.com.R;
import com.devspark.appmsg.AppMsg;
import com.google.analytics.tracking.android.EasyTracker;

import de.greenrobot.event.EventBus;
import de.greenrobot.event.util.AsyncExecutor;
import de.greenrobot.event.util.AsyncExecutor.RunnableEx;

public class CardPaymentActivity extends Activity implements OnClickListener {
	
	private BackgroundTasks bgTask;
	private AQuery aq;
	private Form form;
	private Spinner spnCardType;
	private EditText editCardSerial;
	private EditText editCardCode;
	private Button btnConfirm;
	private ProgressDialog pDialog;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		this.getWindow().getAttributes().windowAnimations = android.R.style.Animation_Dialog;
		this.getWindow().requestFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.layout_card);
		spnCardType = (Spinner) findViewById(R.id.spn_card_type);
		editCardSerial = (EditText) findViewById(R.id.edit_card_serial);
		editCardCode = (EditText) findViewById(R.id.edit_card_code);
		btnConfirm = (Button) findViewById(R.id.btn_confirm);
		EasyTracker.getInstance().setContext(this);
		bgTask = new BackgroundTasks(this);
		aq = new AQuery(this);
		aq.id(btnConfirm).clicked(this);
		Intent receivedData=getIntent();
		if(receivedData.hasExtra("serial"))
		{
			editCardSerial.setText(receivedData.getStringExtra("serial"));
			editCardCode.setText(receivedData.getStringExtra("code"));
		}
		if(receivedData.hasExtra("cardcode"))
		{
			spnCardType.setSelection(receivedData.getIntExtra("cardcode", 0));
			spnCardType.setSelected(false);
		}
		form = new Form();
		Validate serialValid = new Validate(editCardSerial);
		serialValid.addValidator(new NotEmptyValidator(this));
		Validate codeValid = new Validate(editCardCode);
		codeValid.addValidator(new NotEmptyValidator(this));
		form.addValidates(serialValid);
		form.addValidates(codeValid);
	}

	@Override
	protected void onPause() {
		// TODO Auto-generated method stub
		super.onPause();
		EventBus.getDefault().unregister(this);
	}

	@Override
	protected void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
		EventBus.getDefault().register(this);
	}
	
	@Override
	protected void onStart() {
		// TODO Auto-generated method stub
		super.onStart();
		EasyTracker.getInstance().activityStart(this);
	}

	@Override
	protected void onStop() {
		// TODO Auto-generated method stub
		super.onStop();
		EasyTracker.getInstance().activityStop(this);
	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		if(form.validate()){
			AlertDialogManager am = new AlertDialogManager(this);
			am.showButtonsDialog(R.string.confirm_charge, new DialogInterface.OnClickListener() {
				@Override
				public void onClick(DialogInterface dialog, int which) {
					// TODO Auto-generated method stub
					String vendorStr = "";
					switch (spnCardType.getSelectedItemPosition()) {
					case 0:
						vendorStr = "vinaphone";
						break;
					case 1:
						vendorStr = "mobifone";
						break;
					case 2:
						vendorStr = "viettel";
						break;
					case 3:
						vendorStr = "fpt";
						break;
					case 4:
						vendorStr = "vtc";
						break;
					case 5:
						vendorStr = "mega";
						break;
					}
					switch (which) {
					case DialogInterface.BUTTON_POSITIVE:
						pDialog = ProgressDialogManager.showProgressDialog(CardPaymentActivity.this, R.string.loading);
						String accessToken = Utils.getStaticAccessToken(CardPaymentActivity.this);
						EasyTracker.getTracker().sendEvent(Constants.BUY_EVENT_ACTION, Constants.BUY_TYM, "buy tym by card", Long.valueOf(1));
						bgTask.buyTYMCard("", accessToken, editCardSerial.getText().toString(), editCardCode.getText().toString(), vendorStr);
						break;
					case DialogInterface.BUTTON_NEGATIVE:
						dialog.dismiss();
						break;
					}
				}
			});
		}
	}
	
	public void onEventMainThread(final CardSuccessEvent event){
		if(pDialog.isShowing()){
			pDialog.dismiss();
		}
		Toast.makeText(this, R.string.request_send, Toast.LENGTH_SHORT).show();
		final String accessToken = Utils.getStaticAccessToken(this);
		AsyncExecutor.create().execute(new RunnableEx() {
			@Override
			public void run() throws Exception {
				// TODO Auto-generated method stub
				bgTask.checkTopupResult(accessToken, event.getTopupId());
			}
		});
		finish();
	}
	
	public void onEventMainThread(CardFailEvent e){
		if(pDialog.isShowing()){
			pDialog.dismiss();
		}
		AppMsg.makeText(this, e.getMessage(), AppMsg.STYLE_ALERT, null).show();
	}
	
	public void onEventMainThread(RequestFailEvent e){
		if(pDialog.isShowing()){
			pDialog.dismiss();
		}
		ErrorHandler.getInstance().setContext(this).handleError(e.getErrorCode());
	}

}
